package vip.ola.code.common.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import vip.ola.code.secruity.JwtUser;
import vip.ola.core.common.util.GoogleAuthenticator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static vip.ola.core.common.constant.Constant.MAX_PAGE_SIZE;

/**
 * @author: dingzhiwei
 * @date: 17/12/6
 * @description:
 */
public abstract class BaseController {

    private static final int DEFAULT_PAGE_INDEX = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;
    protected static final String PAGE_COMMON_ERROR = "common/error";
    protected static final String PAGE_COMMON_PC_ERROR = "common/pc_error";

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;


    protected JwtUser getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
        return jwtUser;
    }

    protected int getPageIndex() {
        String page = request.getParameter("page");
        if (StringUtils.isBlank(page)) {
            return DEFAULT_PAGE_INDEX;
        }
        return Integer.parseInt(page);
    }

    protected int getPageSize() {
        String limit = request.getParameter("limit");
        if (StringUtils.isBlank(limit)) {
            return DEFAULT_PAGE_SIZE;
        }
        int pageSize = Integer.parseInt(limit);
        if (pageSize > MAX_PAGE_SIZE) {
            return DEFAULT_PAGE_SIZE;
        }
        return pageSize;
    }

    protected String getStringRequired(String key) {
        String value = getString(key);
        if (StringUtils.isBlank(value)) {
            throw new RuntimeException(getErrMsg(key));
        }
        return value;
    }

    protected String getString(String key) {
        return request.getParameter(key);
    }

    protected String getStringDefault(String key, String defaultValue) {
        String value = getString(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return value;
    }

    protected Integer getIntegerRequired(String key) {
        String value = getStringRequired(key);
        return Integer.valueOf(value);
    }

    protected Integer getIntegerDefault(String key, Integer defaultValue) {
        String value = getString(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Integer.valueOf(value);
    }

    protected Long getLongRequired(String key) {
        String value = getStringRequired(key);
        return Long.valueOf(value);
    }

    protected Long getLongDefault(String key, Long defaultValue) {
        String value = getString(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Long.valueOf(value);
    }

    protected Long getBooleanRequired(String key) {
        String value = getStringRequired(key);
        return Long.valueOf(value);
    }

    protected Long getBooleanDefault(String key, Long defaultValue) {
        String value = getString(key);
        if (StringUtils.isBlank(value))
            return defaultValue;
        return Long.valueOf(value);
    }

    private String getErrMsg(String key) {
        return "参数" + key + "必填";
    }

    /**
     * 验证谷歌验证码
     *
     * @param googleAuthSecretKey
     * @param code
     * @return
     */
    protected boolean checkGoogleCode(String googleAuthSecretKey, Long code) {
        if (StringUtils.isBlank(googleAuthSecretKey)) {
            return false;
        }
        // 谷歌验证
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5);    // 最多可偏移的时间
        return ga.check_code(googleAuthSecretKey, code, t);
    }

    /**
     * 得到前端传入的金额元,转换成长整型分
     *
     * @param name
     * @return
     */
    public Long getRequiredAmountL(String name) {
        String amountStr = getStringRequired(name);  // 前端填写的为元,可以为小数点2位
        Long amountL = new BigDecimal(amountStr.trim()).multiply(new BigDecimal(100)).longValue(); // // 转成分
        return amountL;
    }

    public BigDecimal getBigDecimalRequired(String name) {
        String value = getStringRequired(name);
        if(value == null){
            throw new RuntimeException(getErrMsg(name));
        }
        return new BigDecimal(value);
    }

    public BigDecimal getBigDecimal(String name) {
        String amountStr = getString(name);
        if(StringUtils.isBlank(amountStr)){
            return null;
        }
        return new BigDecimal(amountStr);
    }

    public Map<String, Object> request2payResponseMap(HttpServletRequest request, String[] paramArray) {
        Map<String, Object> responseMap = new HashMap<>();
        for (int i = 0; i < paramArray.length; i++) {
            String key = paramArray[i];
            String v = request.getParameter(key);
            if (v != null) {
                responseMap.put(key, v);
            }
        }
        return responseMap;
    }

}
